package com.jfinal.code.eclipse.plugin.wizards.page;

import java.sql.Connection;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.List;

import com.jfinal.code.eclipse.plugin.generator.db.DbType;
import com.jfinal.code.eclipse.plugin.generator.db.DbUtils;
import com.jfinal.code.eclipse.plugin.i18n.I18n;
import com.jfinal.code.eclipse.plugin.wizards.ui.TableSelectUI;

/**
 * 生成代码数据库表选择引导页
 * @author yaolin
 *
 */
public class TableSelectWizardPage extends WizardPage {

	private static final String PAGE_NAME = "TableSelectWizardPage";
	private ISelection selection;
	
	private List ordinalList;// 原始列表
	private List selectedList; // 已经选择的列表
	private Button allToRightButton;// 全部选择
	private Button toRightButton;// 选择
	private Button toLeftButton; // 取消选择
	private Button allToLeftButton; // 全部取消
	private String[] listItems; // 数据库表
	public TableSelectWizardPage(ISelection selection) {
		super(PAGE_NAME);
		this.selection = selection;
		setTitle(I18n.tableSelectWp_title);
		setDescription(I18n.tableSelectWp_description);
	}

	@Override
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout(3, false);
		container.setLayout(layout);
		layout.verticalSpacing = 10;
		layout.horizontalSpacing = 3;
		
		// UI
		TableSelectUI ui = new TableSelectUI(this, getShell());
		
		// 1.左列表
		Group leftGroup = new Group(container, SWT.NULL);
		leftGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
		leftGroup.setText(I18n.tableSelectWp_databaseTable);
		leftGroup.setLayout(new GridLayout(1, false));
		ordinalList = ui.createOrdinalList(leftGroup);
		// 2.按钮
		{ // 列表中间的按钮组
			Composite btnGroup = new Composite(container, SWT.NULL);
			GridLayout btnGroupLayout = new GridLayout(1, false);
			btnGroup.setLayout(btnGroupLayout);
			btnGroupLayout.verticalSpacing = 9;
			allToRightButton = ui.createAllToRightButton(btnGroup);
			toRightButton = ui.createToRightButton(btnGroup);
			toLeftButton = ui.createToLeftButton(btnGroup);
			allToLeftButton = ui.createAllToLeftButton(btnGroup);
		}
		// 3.右列表
		Group rightGroup = new Group(container, SWT.NULL);
		rightGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
		rightGroup.setText(I18n.tableSelectWp_selectedTable);
		rightGroup.setLayout(new GridLayout(1, false));
		selectedList = ui.createSelectedList(rightGroup);
		setControl(container);
	}

	/**
	 * 通过重写setVisible获取数据信息页的信息，并进行处理
	 */
	@Override
	public void setVisible(boolean visible) {
		beforeVisible(); // 获取数据库信息并进行处理
		super.setVisible(visible);
	}
	
	/*
	 * beforeVisible 获取数据库信息并进行处理
	 */
	private void beforeVisible() {
		String message = null;
		try {
			DatabaseInfoWizardPage prevPage = (DatabaseInfoWizardPage) getPreviousPage();
			int i = prevPage.getDatabaseTypeCombo().getSelectionIndex();
			String jarFile = prevPage.getDriverLocationText().getText();
			String driverClassName = prevPage.getJdbcDriverClassNameText().getText();
			String url = prevPage.getJdbcUrlText().getText();
			String username = prevPage.getUsernameText().getText();
			String password = prevPage.getPasswordText().getText();
			Connection conn = DbUtils.getConnection(jarFile, driverClassName, url, username, password);
			DbUtils.listTable(DbType.getByOrdinal(i), conn);
			listItems = DbUtils.DbInfoHolder.listTableName();
			getOrdinalList().setItems(listItems);
			DbUtils.closeConnection(conn);
		} catch (Throwable e) {
			message = I18n.error_can_not_connection_to_database + " : " + e.getMessage();
		}
		setErrorMessage(message);
		setPageComplete(message == null);
		if (message == null) {
			TableSelectUI.Validator.validateEmptyItem(this);
		}
	}
	
	// getter
	public ISelection getSelection() {
		return selection;
	}
	public List getOrdinalList() {
		return ordinalList;
	}
	public List getSelectedList() {
		return selectedList;
	}
	public Button getAllToRightButton() {
		return allToRightButton;
	}
	public Button getToRightButton() {
		return toRightButton;
	}
	public Button getToLeftButton() {
		return toLeftButton;
	}
	public Button getAllToLeftButton() {
		return allToLeftButton;
	}
	public String[] getListItems() {
		return listItems;
	}
}
